<!DOCTYPE html>
<html>
<body>
<script src="/js-test-resources/js-test.js"></script>
<script>
description("WebGL's tex(Sub)Image2D should throw a SecurityError exception when the ImageBitmap is not origin clean.");

window.jsTestIsAsync = true;
var gl;
var bitmap;

var image = document.createElement('img');
image.src = 'http://localhost:8080/security/resources/abe.png';

image.addEventListener('load', function() {
    var canvas = document.createElement("canvas");
    canvas.width = 10;
    canvas.height = 10;

    gl = canvas.getContext("webgl");
    var texture = gl.createTexture();
    shouldBe("gl.getError()", "gl.NO_ERROR");
    gl.bindTexture(gl.TEXTURE_2D, texture);
    shouldBe("gl.getError()", "gl.NO_ERROR");

    // ImageBitmap created from a clean canvas should be origin clean
    createImageBitmap(canvas, 0, 0, 10, 10).then(imageBitmap => {
        bitmap = imageBitmap;
        shouldNotThrow("gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmap)");
        shouldBe("gl.getError()", "gl.NO_ERROR");
        shouldNotThrow("gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, bitmap)");
        shouldBe("gl.getError()", "gl.NO_ERROR");

        // Test tainted ImageBitmap
        createImageBitmap(image, 0, 0, 10, 10).then(imageBitmap => {
            bitmap = imageBitmap;
            shouldThrow("gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmap)");
            shouldBe("gl.getError()", "gl.NO_ERROR");
            shouldThrow("gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, bitmap)");
            shouldBe("gl.getError()", "gl.NO_ERROR");
            finishJSTest();
        }, () => {
            testFailed("Unexpected failure");
            finishJSTest();
        });
    }, () => {
        testFailed("Unexpected failure");
        finishJSTest();
    });
});
</script>
</body>
</html>
